package com.winning.devops.batch.nested.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.JobStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * @author chensj
 * @date 2020-02-28 18:07
 */
@Configuration
@EnableBatchProcessing
public class MultiJobDemo {


    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private JobLauncher jobLauncher;
    @Autowired
    @Qualifier("demoChildJobOne")
    private Job demoChildJobOne;
    @Autowired
    @Qualifier("demoChildJobTwo")
    private Job demoChildJobTwo;

    @Bean
    public Job parentJob(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return jobBuilderFactory.get("parentJob")
                .start(childJobOne(jobRepository, transactionManager))
                .next(childJobTwo(jobRepository, transactionManager))
                .build();
    }

    /**
     * 返回的是Job类型的Step，特殊类型的step
     * 将Job类型转换为step类型
     *
     * @param jobRepository      持久化
     * @param transactionManager 事务管理
     * @return step
     */
    private Step childJobTwo(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        // 使用JobStepBuilder构建Step
        return new JobStepBuilder(new StepBuilder("childJobTwo"))
                // 指定job的bean
                .job(demoChildJobTwo)
                // 指定Job的启动者，来源父Job的启动对象
                .launcher(jobLauncher)
                // 指定持久化对象
                .repository(jobRepository)
                // 事务管理器
                .transactionManager(transactionManager)
                .build();
    }

    /**
     * 返回的是Job类型的Step
     * 将Job类型转换为step类型
     *
     * @param jobRepository      持久化
     * @param transactionManager 事务管理
     * @return step
     */
    private Step childJobOne(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        // 使用JobStepBuilder构建Step
        return new JobStepBuilder(new StepBuilder("childJobOne"))
                // 指定job的bean
                .job(demoChildJobOne)
                // 指定Job的启动者，来源父Job的启动对象
                .launcher(jobLauncher)
                // 指定持久化对象
                .repository(jobRepository)
                // 事务管理器
                .transactionManager(transactionManager)
                .build();
    }
}
